/*
 * Copyright (C) 2015  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


/**
 * @file
 * @brief Functions to manage multiple 2D positions of unsigned int.
 */


#ifndef PLANET_WARS_2D_RT_CORE_POSITIONS_2D_UINT_H
#define PLANET_WARS_2D_RT_CORE_POSITIONS_2D_UINT_H


#include "PlanetWars2dRT-core/utils/position_2d_uint.h"
#include <stddef.h>


EXTERN_C_BEGIN


static inline
position_2d_uint
positions_2d_uint_get_center(const position_2d_uint* positions, size_t nb)
{
  position_2d_uint position_center;
  if(positions == NULL || nb == 0)
    {
      position_center.x = 0;
      position_center.y = 0;
      return position_center;
    }
  
  position_center.x = positions[0].x;
  position_center.y = positions[0].y;
  for(size_t i=1; i < nb; ++i)
    {
      position_center.x += positions[i].x;
      position_center.y += positions[i].y;
    }
  position_center.x /= nb;
  position_center.y /= nb;
  return position_center;
}

static inline
position_2d_uint
positions_2d_uint_get_nearest_of_an_other_position(const position_2d_uint* positions,
						   size_t nb,
						   const position_2d_uint* other_position)
{
  position_2d_uint position;
  if(positions == NULL || nb == 0)
    {
      position.x = 0;
      position.y = 0;
      return position;
    }
  
  position = positions[0];
  for(size_t i=1; i < nb; ++i)
    {
      if(distance_between_2_positions_2d_uint(&positions[i],
					      other_position)
	 <
	 distance_between_2_positions_2d_uint(&positions[i],
					      &position))
	{
	  position = positions[i];
	}
    }
  return position;
}


EXTERN_C_END


#endif
